import { Module } from '@nestjs/common';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { UploadController } from './upload.controller';
import { UploadService } from './upload.service';
import { extname } from 'path';

@Module({
    imports: [
        MulterModule.register({
            storage: diskStorage({
                destination: './uploads',
                filename: (req, file, callback) => {
                    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
                    callback(null, `${uniqueSuffix}${extname(file.originalname)}`);
                },
            }),
            fileFilter: (req, file, callback) => {
                if (!file.originalname.match(/\.(jpg|jpeg|png|gif|webp|bmp|svg)$/i)) {
                    return callback(new Error('不支持的文件类型，请上传图片文件'), false);
                }
                if (!file.mimetype.match(/^image\//)) {
                    return callback(new Error('请上传有效的图片文件'), false);
                }
                callback(null, true);
            },
            limits: {
                fileSize: 10 * 1024 * 1024, // 增加到 10MB
            },
        }),
    ],
    controllers: [UploadController],
    providers: [UploadService],
})
export class UploadModule { } 